package com.itstu.gulimall.order.service.impl;

import com.itstu.gulimall.order.entity.OrderEntity;
import com.itstu.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itstu.common.utils.PageUtils;
import com.itstu.common.utils.Query;

import com.itstu.gulimall.order.dao.OrderItemDao;
import com.itstu.gulimall.order.entity.OrderItemEntity;
import com.itstu.gulimall.order.service.OrderItemService;


@Service("orderItemService")
@RabbitListener(queues = {"hello-java-queue"})
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queue声明要监听的所有队列
     * 参数可以参考以下类型
     * 1. Message message ： 原生消息详请 class org.springframework.amqp.core.Message
     * 2. T<发送消息的类型></>
     * 3. Channel channel : 当前传播数据的通道
     * 4.
     *
     * Queue，可以很多人来监听，只要收到消息，队列就会删除消息，而却只能有一个收到消息
     * 场景
     *      1） 订单服务启动多个，同一个消息只能有一个客户端收到
     *      2)  只有一个消息完全处理完，方法运行结束，我们才能接收下一个消息
     *
     */
//    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void reciveMessage(Message message,
                              OrderReturnReasonEntity content,
                              Channel channel)  {
//        byte[] body = message.getBody();
        System.out.println("接收消息的内容"+content.getName()+"==>类型: "+content.getClass());
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // deliveryTag是自增的
        System.out.println(deliveryTag);
        try {
            if (deliveryTag%2 == 0){
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了货物。。。"+deliveryTag);
            }else {
                // 退货
                channel.basicNack(deliveryTag,false,true);
                System.out.println("没有签收货物。。。"+deliveryTag);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("消息处理完成=>"+content.getName());

    }
    @RabbitHandler
    public void reciveMessage2(OrderEntity content)  {
//        byte[] body = message.getBody();
        System.out.println("接收消息的内容"+content.getReceiverName()+"==>类型: "+content.getClass());
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println("消息处理完成=>"+content.getOrderSn());
    }

}